package com.example.sharding.algorithms;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 精准分片
 * <p>
 * 泛型参数为分片键的数据类型
 *
 * @author zxs
 * @since 2021/9/22
 */
public class OrderPreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    /**
     * 分片算法
     *
     * @param availableTargetNames 1.如果将该算法配置为DatabaseStrategy,则availableTargetNames的值为可选的数据源名
     *                             2.如果将该算法配置为TableStrategy,则availableTargetNames的值为可选的表名
     * @param shardingValue        分片键的值
     * @return sharding result for data source or table's name
     */
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        String result;
        switch (shardingValue.getValue()) {
            case "SELL":
                result = "0";
                break;
            case "MAIL":
                result = "1";
                break;
            default:
                result = "0";
        }
        return "d" + result;
    }
}
